package org.piax.rospubsub;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;

public class Shell {
    public static void main(String[] args) {
        // 設定
        // TODO ファイル等から読ませるべき
        int publish_ctrl_port = 8080;
        String ros_master_host = "localhost";
        int ros_master_port = 8888;
        String adb_forward_host = "localhost";
        int adb_forward_port = 8000;

        try {
            InetSocketAddress ros_master = new InetSocketAddress(ros_master_host, ros_master_port);
            InetSocketAddress adb_fwd = new InetSocketAddress(adb_forward_host, adb_forward_port);
            ROSDTNProxy proxy = new ROSDTNProxy(ros_master, publish_ctrl_port, adb_fwd);
            proxy.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            // 終了指示待ちループ
            System.out.println("Start a shell. To escape this, enter 'bye'.");
            while (true) {
                try {
                    System.out.print("Input Command >");

                    String input = reader.readLine();
                    if (input == null) {
                        Thread.sleep(500);
                        continue;
                    }
                    String[] cmds = input.split("\\s+");

                    if (cmds.length == 0 || "".equals(cmds[0])) {
                        continue;
                    }
                    if (cmds[0].equals("bye")) {
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
            proxy.stop();
            System.out.println("Exit shell.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
